<template>
  <div class="topic-management">
    <el-card class="filter-card">
      <el-form :inline="true">
        <el-form-item label="审核状态">
          <el-select 
            v-model="filterStatus" 
            placeholder="全部状态"
            clearable
            style="width: 100px;"
          >
            <el-option label="待审核" :value="0" />
            <el-option label="已通过" :value="1" />
            <el-option label="已拒绝" :value="2" />
          </el-select>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            @click="fetchTopics"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-table 
        :data="filteredTopics" 
        v-loading="loading"
        style="width: 100%"
      >
        <el-table-column prop="topicId" label="ID" width="80" />
        
        <el-table-column label="话题内容" min-width="200">
          <template #default="{ row }">
            <div class="topic-content">
              {{ row.topic }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="用户信息" width="180">
          <template #default="{ row }">
            <div class="user-info">
              <el-avatar :src="'http://localhost:8092' + row.avatar" />
              <div class="user-meta">
                <div>{{ row.nickname }}</div>
                <div class="user-id">ID:{{ row.userId }}</div>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="createdAt" label="创建时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.createdAt) }}
          </template>
        </el-table-column>

        <el-table-column label="审核状态" width="120">
          <template #default="{ row }">
            <el-tag :type="statusTagType(row.approvalStatus)">
              {{ statusText(row.approvalStatus) }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button 
              v-if="row.approvalStatus === 0"
              type="success" 
              size="small"
              @click="handleApprove(row.topicId, 1)"
            >
              通过
            </el-button>
            <el-button 
              v-if="row.approvalStatus === 0"
              type="danger" 
              size="small"
              @click="handleApprove(row.topicId, 2)"
            >
              拒绝
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          :current-page="pagination.current"
          :page-size="pagination.size"
          :total="pagination.total"
          @current-change="handlePageChange"
          layout="total, prev, pager, next"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const loading = ref(false)
const topics = ref([])
const filterStatus = ref(null)

const pagination = ref({
  current: 1,
  size: 10,
  total: 0
})

// 格式化时间
const formatDate = (dateStr) => {
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 状态显示
const statusText = (status) => {
  return {
    0: '待审核',
    1: '已通过',
    2: '已拒绝'
  }[status] || '未知状态'
}

// 状态标签样式
const statusTagType = (status) => {
  return {
    0: 'warning',
    1: 'success',
    2: 'danger'
  }[status] || 'info'
}

// 获取话题列表
const fetchTopics = async () => {
  try {
    loading.value = true
    const params = {
      page: pagination.value.current,
      pageSize: pagination.value.size,
      status: filterStatus.value
    }

    const { data } = await axios.get('http://localhost:8092/topic/list', { params })
    
    if (data.code === 200) {
      topics.value = data.data
      pagination.value.total = data.total || 0
    }
  } catch (error) {
    ElMessage.error('获取话题失败')
  } finally {
    loading.value = false
  }
}

// 处理审核
const handleApprove = async (topicId, status) => {
  try {
    await axios.post('http://localhost:8092/topic/admin/approve-topic', 
      null, // 请求体为空
      {
        params: {  // 将参数放在params配置中
          topicId: topicId,
          approvalStatus: status
        },
        headers: {
          Authorization: `Bearer ${localStorage.getItem('token')}`
        }
      }
    )
    
    ElMessage.success('操作成功')
    await fetchTopics()
  } catch (error) {
    ElMessage.error(`操作失败: ${error.response?.data?.msg || error.message}`)
  }
}
// 分页变化
const handlePageChange = (page) => {
  pagination.value.current = page
  fetchTopics()
}

// 过滤后的数据
const filteredTopics = computed(() => {
  return topics.value.filter(topic => {
    if (filterStatus.value === null) return true
    return topic.approvalStatus === filterStatus.value
  })
})

onMounted(() => {
  fetchTopics()
})
</script>

<style scoped>
.topic-management {
  padding: 20px;
}

.filter-card {
  margin-bottom: 20px;
}

.table-card {
  margin-top: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  .el-avatar {
    margin-right: 10px;
  }
}

.user-meta {
  line-height: 1.4;
  .user-id {
    font-size: 12px;
    color: #999;
  }
}

.topic-content {
  line-height: 1.6;
  word-break: break-word;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-table {
  :deep(.cell) {
    padding: 12px 0;
  }
}
</style>